<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="领料时间" prop="startdate">
        <el-date-picker clearable size="small" style="width: 200px" v-model="queryParams.startDate" type="date" value-format="yyyy-MM-dd" placeholder="选择开始日期">
            </el-date-picker>
            <el-date-picker clearable size="small" style="width: 200px" v-model="queryParams.endDate" type="date" value-format="yyyy-MM-dd" placeholder="选择结束日期">
            </el-date-picker>
      </el-form-item>
      <el-form-item label="领料单号" prop="picklistCode">
        <el-input
          v-model="queryParams.picklistCode"
          placeholder="请输入领料单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库房" prop="srguid">
        <el-select v-model="queryParams.srguid" placeholder="请选择库房" clearable size="small">
          <el-option
              v-for="item in storeroomOptions"
              :key="item.guid"
              :label="item.name"
              :value="item.guid"
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" filterable clearable>
            <el-option
              v-for="item in pickOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

   <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bill:producereport:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bill:producereport:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bill:producereport:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bill:producereport:exportplanlist']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="producereportList" @selection-change="handleSelectionChange">
      <el-table-column label="货品工厂" align="center" prop="factory" />
      <el-table-column label="库房" align="center" prop="storeroom" width="140" />
      <el-table-column label="领料单编号" align="center" prop="picklistCode" />
      <el-table-column label="领料时间" align="center" prop="pickingdate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.pickingdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出库品种" align="center" prop="product" />
      <el-table-column label="出库规格" align="center" prop="outSpename" width="140" />
      <el-table-column label="出库重量" align="center" prop="outWeight" />
      <el-table-column label="入库品种" align="center" prop="inProduct" />
      <el-table-column label="入库规格" align="center" prop="inSpename" />
      <el-table-column label="入库重量" align="center" prop="inWeight" />
      <el-table-column label="损耗明细" align="center" prop="itemLossWeight" />
      <el-table-column label="退料重量" align="center" prop="returnWeight" />
      <el-table-column label="报废重量" align="center" prop="scrapWeight" />
      <el-table-column label="损耗重量" align="center" prop="lossWeight" />
      <el-table-column label="状态" align="center" prop="statusname" />
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="100%" append-to-body>
      <el-table v-loading="loading" :data="producereportitemList" @selection-change="handleSelectionChange">
      <el-table-column label="部门名称" align="center" prop="departname" />
      <el-table-column label="出库房名称" align="center" prop="srname" />
      <el-table-column label="出库间名称" align="center" prop="shrname" />
      <el-table-column label="出库产品" align="center" prop="outproduct" />
      <el-table-column label="出库规格" align="center" prop="outspecname" />
      <el-table-column label="入库产品" align="center" prop="inproduct" />
      <el-table-column label="入库规格" align="center" prop="inspecname" />
      <!-- <el-table-column label="入库规格类型" align="center" prop="insptype" /> -->
      <el-table-column label="入库" align="center" prop="inweight" />
      <el-table-column label="出库" align="center" prop="outweight" />
      <el-table-column label="损耗" align="center" prop="lossweight" />
      <el-table-column label="退料" align="center" prop="returnweight" />
      <el-table-column label="报废" align="center" prop="scrapweight" />
      <el-table-column label="入库单位" align="center" prop="instockunit" />
      <el-table-column label="出库单位" align="center" prop="outstockunit" />
      <el-table-column label="成本对象" align="center" prop="cospname" />
    </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { planlist, exportplanlist } from "@/api/bill/producereport";
import { findStoreroomListByPermiss } from "@/api/public";

export default {
  name: "ProducePlanReport",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产报表格数据
      producereportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        startDate: this.getMonthFirDate(),
        endDate: null,
        picklistCode: null,
        status: null,
        srguid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      producereportitemList: [],
      storeroomOptions:[],
      pickOptions:[],
    };
  },
  created() {
    this.getList();
    this.findStoreroom();
    this.getDicts("pick_status").then(response => {
      this.pickOptions = response.data;
    });
  },
  methods: {
    findStoreroom(){
      findStoreroomListByPermiss({functionType:8}).then(response => {
        this.storeroomOptions = response.data;
      });
    },
    /** 查询生产报列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams)
      planlist(this.queryParams).then(response => {
        this.producereportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        plcode: null,
        plstatus: 0,
        pldate: null,
        orgname: null,
        orgguid: null,
        inweight: null,
        outweight: null,
        backweight: null,
        starttime: null,
        endtime: null,
        creattime: null,
        departid: null,
        departname: null,
        srguid: null,
        srname: null,
        planguid: null,
        orderdate: null,
        operatorid: null,
        intype: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleDetail(row){
      listProducereportitem({producereportid:row.id}).then(response => {
        this.producereportitemList = response.rows;
        this.open = true;
      this.title = "生产报表明细";
        });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProducereport(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产报";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProducereport(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProducereport(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProducereport(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有生产计划报表数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportplanlist(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
